<?php

/**
 * usuario actions.
 *
 * @package    SCCEIC
 * @subpackage usuario
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class usuarioActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->usuario_list = UsuarioPeer::doSelect(new Criteria());
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new UsuarioForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));

	$mailTo = $request->getParameter('usuario');
	$newPass=substr(sha1($mailTo['mail'].time()),0,5);
	$mailTo['password']=$newPass;
	$mailBody = $this->getPartial('mail', array('pass' => $newPass,'name' => $mailTo['nombre']));
	$request->setParameter('usuario',$mailTo);
	$this->res=$mailTo ;
	$mailTo=$mailTo['mail'];
	$mailFrom="ceic@ldc.usb.ve";
	$this->res=$mailTo ;
	$this->form = new UsuarioForm(); 
	$this->valid=$this->processForm2($request, $this->form);
	$this->setTemplate('create');		    		  

	if ($this->valid!=-1){
		try
		{
		  $mailer = new Swift(new Swift_Connection_SMTP('smtp.ldc.usb.ve'));		  
		  $message = new Swift_Message('Confirmacion de Registro', $mailBody, 'text/html');
		  $mailer->send($message, $mailTo, $mailFrom);
		  $mailer->disconnect();
		}
		catch (Exception $e)
		{
		  $mailer->disconnect();
		}		
	}
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($usuario = UsuarioPeer::retrieveByPk($request->getParameter('mail')), sprintf('Object usuario does not exist (%s).', $request->getParameter('mail')));
    $this->form = new UsuarioForm($usuario);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($usuario = UsuarioPeer::retrieveByPk($request->getParameter('mail')), sprintf('Object usuario does not exist (%s).', $request->getParameter('mail')));
    $this->form = new UsuarioForm($usuario);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($usuario = UsuarioPeer::retrieveByPk($request->getParameter('mail')), sprintf('Object usuario does not exist (%s).', $request->getParameter('mail')));
    $usuario->delete();

    $this->redirect('usuario/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $usuario = $form->save();

      $this->redirect('usuario/edit?mail='.$usuario->getMail());
    }
  }
    protected function processForm2(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $usuario = $form->save();
      return 0;
    }
    return -1;
  }
}
